package cn.edu.eight.food.controller;

import cn.edu.eight.food.model.OfOrder;
import cn.edu.eight.food.model.OfUser;
import cn.edu.eight.food.model.Order;
import cn.edu.eight.food.service.ofOrderService;
import cn.edu.eight.food.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class ofOrderController {
    @Autowired
    ofOrderService ofOrderService;

    //跳转到我的订单页面
    @RequestMapping("/toOrder")
    private String toOrder(HttpSession session, HttpServletRequest request){
        OfUser user=(OfUser) session.getAttribute("loginUser");
        List<Map> mapList=ofOrderService.orderList(user.getOfUserId());
        request.setAttribute("mapList",mapList);
        System.out.println(mapList);
        return "order";
    }

    //跳转到订单管理
    @RequestMapping("/orderList")
    @ResponseBody
    private Result toOrders(String orderId,String userId,String mobile){
        List<Map> mapList=ofOrderService.orderListAll(orderId,userId,mobile);
        Result<Map> mapResult = new Result<>();
        if (mapList != null && mapList.size() > 0){
            mapResult.setCode("0");
            mapResult.setMsg("请求成功");
            mapResult.setCount(mapList.size());
            mapResult.setData(mapList);
        }else {
            mapResult.setCode("500");
            mapResult.setMsg("没有数据！");
        }
        return mapResult;
    }

    //跳转到购物车页面
    @RequestMapping("/toCart")
    private String toCart(HttpSession session, HttpServletRequest request){
        OfUser user=(OfUser) session.getAttribute("loginUser");
        if (user == null){
            return "login";
        }else {
            List<Map> mapList=ofOrderService.cartList(user.getOfUserId());
            request.setAttribute("mapList",mapList);
            System.out.println(mapList);
            return "cart";
        }
    }

    //修改订单信息
    @RequestMapping("/saveCart")
    @ResponseBody
    public Result saveCart(@RequestBody OfOrder ofOrder){
        Result result=new Result();
        ofOrderService.saveCart(ofOrder);
        result.setCode("200");
        return result;
    }

    @RequestMapping("/delCart")
    @ResponseBody
    public Result delCart(String orderId){
        Result result = new Result();
        ofOrderService.delCart(orderId);
        result.setCode("200");
        return result;
    }

    @RequestMapping("/buyOrder")
    private String buyOrder(@RequestParam String foodId, HttpSession session){
        OfUser user=(OfUser) session.getAttribute("loginUser");
        if (user == null){
            return "login";
        }else {
            String[] ids=new String[1];
            ids[0] = foodId;
            String[] nums=new String[1];
            nums[0] = "1";
            ofOrderService.buyFood(user,ids,nums);
            return "redirect:/index/payment";
        }
    }

    @RequestMapping("/addCart")
    private String addOrder(@RequestParam String foodId, HttpSession session){
        Date d=new Date();
        OfUser user=(OfUser) session.getAttribute("loginUser");
        if (user == null){
            return "login";
        }else {
            OfOrder ofOrder=ofOrderService.findById(foodId,user.getOfUserId());
            if (ofOrder!=null){
                if (ofOrder.getOfOrderNumber()==5){
                    ofOrder.setOfOrderNumber(ofOrder.getOfOrderNumber());
                }else {
                    ofOrder.setOfOrderNumber(ofOrder.getOfOrderNumber()+1);
                }
                ofOrderService.saveCart(ofOrder);
            }else {
                OfOrder ofOrder1=new OfOrder();
                long id = d.getTime();
                ofOrder1.setOfOrderId((int)id);
                ofOrder1.setOfOrderFoodId(Integer.parseInt(foodId));
                ofOrder1.setOfOrderUserId(user.getOfUserId());
                ofOrder1.setOfOrderNumber(1);
                ofOrder1.setOfOrderState(1);
                ofOrderService.insert(ofOrder1);
            }
            return "redirect:/order/toCart";
        }
    }

    //购物车结算
    @RequestMapping("/buyCart")
    @ResponseBody
    public Result buyCart(@RequestBody String[] ids){
        Result result = new Result();
        Date d=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
        OfOrder ofOrder = new OfOrder();
        Order order = new Order();
        for (int i=0;i<ids.length;i++){
            ofOrder.setOfOrderId(Integer.parseInt(ids[i]));
            ofOrder.setOfOrderState(2);
            ofOrderService.saveCart(ofOrder);
            if (order.getOrderContent() == null){
                order.setOrderContent("#"+ofOrder.getOfOrderId());
            }else {
                order.setOrderContent(order.getOrderContent()+"#"+ofOrder.getOfOrderId());
            }
        }
        order.setOrderTime(sdf.format(d));
        ofOrderService.insert(order);
        result.setCode("200");
        return result;
    }
}
